package com.yakami.ocean.view.fragment.base;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.yakami.ocean.R;
import com.yakami.ocean.adapter.NewsAdapter;
import com.yakami.ocean.adapter.base.BaseListAdapter;
import com.yakami.ocean.bean.News;

import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author Yakami, Created on 2016/4/10
 */
public class BaseNewsListFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {

    protected @Bind(R.id.id_recyclerview) RecyclerView mRecyclerView;
    protected @Bind(R.id.swipe_refresh) SwipeRefreshLayout mSwipeRefresh;
    protected @Bind(R.id.load_error) TextView mErrorText;
    protected NewsAdapter mAdapter;
    protected OnRefreshListener mRefreshListener;
    protected BaseListAdapter.OnItemClickListener mItemClickListener;
    protected BaseListAdapter.OnItemLongClickListener mItemLongClickListener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news, container, false);
        ButterKnife.bind(this, view);

        mSwipeRefresh.setColorSchemeResources(R.color.PeterRiver);
        mSwipeRefresh.setOnRefreshListener(this);

        initAdapter();
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setAutoMeasureEnabled(true);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(mAdapter);
        afterCreateView();
        //设置item点击监听
        if (mItemClickListener != null)
            mAdapter.setOnItemClickListener(mItemClickListener);
        if (mItemLongClickListener != null)
            mAdapter.setOnItemLongClickListener(mItemLongClickListener);

        return view;
    }

    public void afterCreateView() {

    }

    public void initAdapter() {
        mAdapter = new NewsAdapter(mAppContext);
    }

    public void setData(List<News> list) {
        mAdapter.clear();
        mAdapter.addItem(list);
    }

    public void setRefreshListener(OnRefreshListener listener) {
        mRefreshListener = listener;
    }

    public void refresh() {
        setRefreshState();
        mRefreshListener.getRemoteData();
        mAdapter.notifyDataSetChanged();
    }

    public void setRefreshState() {
        mSwipeRefresh.setRefreshing(true);
        mSwipeRefresh.setEnabled(false);
    }

    public void onLoading() {
        mSwipeRefresh.setRefreshing(false);
        mSwipeRefresh.setEnabled(true);
        mRecyclerView.setVisibility(View.GONE);
        mErrorText.setText(mRes.getString(R.string.loading));
        mErrorText.setVisibility(View.VISIBLE);
    }

    public void onSuccess() {
        mAdapter.notifyDataSetChanged();
        mSwipeRefresh.setRefreshing(false);
        mSwipeRefresh.setEnabled(true);
        if (mAdapter.getItemCount() == 0) {
            mRecyclerView.setVisibility(View.GONE);
            mErrorText.setText(mRes.getText(R.string.no_data));
            mErrorText.setVisibility(View.VISIBLE);
        } else {
            mRecyclerView.setVisibility(View.VISIBLE);
            mErrorText.setVisibility(View.GONE);
        }
    }

    public void onError() {
        mSwipeRefresh.setRefreshing(false);
        mSwipeRefresh.setEnabled(true);
        mRecyclerView.setVisibility(View.GONE);
        mErrorText.setText(mRes.getString(R.string.load_failed));
        mErrorText.setVisibility(View.VISIBLE);
        RxView.clicks(mErrorText)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe((Void) -> {
                    mErrorText.setVisibility(View.GONE);
                    refresh();
                });
    }

    public void setOnItemClickListener(BaseListAdapter.OnItemClickListener listener) {
        mItemClickListener = listener;
    }

    public void setOnItemLongClickListener(BaseListAdapter.OnItemLongClickListener listener) {
        mItemLongClickListener = listener;
    }

    @Override
    public void onRefresh() {
        refresh();
    }


    public interface OnRefreshListener {
        void getRemoteData();
    }

}
